/**
 *!
 * \file        b_drv_TM7711.c
 * \version     v0.0.1
 * \date        2021/04/23
 * \author      Rain
 *******************************************************************************
 * @attention
 *
 * Copyright (c) 2021 Rain
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *******************************************************************************
 */

/*Includes ----------------------------------------------*/
#include "b_drv_tm7711.h"

#include <string.h>
/**
 * \addtogroup B_DRIVER
 * \{
 */

/**
 * \addtogroup TM7711
 * \{
 */

/**
 * \defgroup TM7711_Private_TypesDefinitions
 * \{
 */

/**
 * \}
 */

/**
 * \defgroup TM7711_Private_Defines
 * \{
 */

/**
 * \}
 */

/**
 * \defgroup TM7711_Private_Macros
 * \{
 */
#define CH1_10HZ 0x01
#define CH1_40HZ 0x02
#define CH2_TEMP 0x03
#define CH1_10HZ_CLK 25
#define CH1_40HZ_CLK 27
#define CH2_TEMP_CLK 26
/**
 * \}
 */

/**
 * \defgroup TM7711_Private_Variables
 * \{
 */
b_tm7711_halif_t  b_tm7711_halif = TM7711_DEFAULT_CONFIG;
b_tm7711_driver_t b_tm7711_driver;
/**
 * \addtogroup TM7711_Exported_Functions
 * \{

*/
#if __GNUC__
void __attribute__((weak)) bTM7711Callback(int value)
#else
__weak void bTM7711Callback(int value)
#endif
{
}

/**
 * \}
 */

/**
 * \defgroup TM7711_Private_FunctionPrototypes
 * \{
 */
static int _b_tm7711_close(b_tm7711_driver_t *pdrv)
{
    b_tm7711_halif_t *b_tm7711_halif = pdrv;

    bHalGPIO_Config(b_tm7711_halif->clck.port, b_tm7711_halif->clck.pin, B_HAL_GPIO_OUTPUT,
                    B_HAL_GPIO_NOPULL);
    bHalGPIO_Config(b_tm7711_halif->data.port, b_tm7711_halif->data.pin, B_HAL_GPIO_INPUT,
                    B_HAL_GPIO_PULLUP);
}
void _bTM7711_Delay(uint32_t i)
{
    for (; i > 0; i--)
    {
        //__nop();
    }
}

static void _b_tm7711_polling(void)
{
    b_tm7711_halif_t *config    = b_tm7711_driver._hal_if;
    unsigned char     i         = 0;
    int               data_temp = 0;
    if (bHalGPIO_ReadPin(config->data.port, config->data.pin) == 0)
    {
        for (i = 0; i < 24; i++)
        {
            bHalGPIO_WritePin(config->clck.port, config->clck.pin,
                              1);  //在global.h文件中定义，臵SCK脚输出高电平

            bUtilDelayUS(10);  //延时5个微秒，根据不同的MCU自定义此函数

            bHalGPIO_WritePin(config->clck.port, config->clck.pin,
                              0);  //在global.h文件中定义，臵SCK脚输出低电平
            if (bHalGPIO_ReadPin(config->data.port, config->data.pin))  //判断DOUT是否为高电平
            {
                data_temp |= 1;
            }
            data_temp <<= 1;
        }
        uint8_t next_select = CH1_10HZ;
        switch (next_select)  //确定下一次数据更新率或者切换通道
        {
            case CH1_10HZ:
                bHalGPIO_WritePin(config->clck.port, config->clck.pin, 1);
                bUtilDelayUS(5);
                bHalGPIO_WritePin(config->clck.port, config->clck.pin, 0);

                break;
            case CH1_40HZ:
                bHalGPIO_WritePin(config->clck.port, config->clck.pin, 1);
                bUtilDelayUS(5);
                bHalGPIO_WritePin(config->clck.port, config->clck.pin, 0);

                bUtilDelayUS(5);
                bHalGPIO_WritePin(config->clck.port, config->clck.pin, 1);
                bUtilDelayUS(5);
                bHalGPIO_WritePin(config->clck.port, config->clck.pin, 0);

                bUtilDelayUS(5);
                bHalGPIO_WritePin(config->clck.port, config->clck.pin, 1);
                bUtilDelayUS(5);
                bHalGPIO_WritePin(config->clck.port, config->clck.pin, 0);

                break;
            case CH2_TEMP:
                bHalGPIO_WritePin(config->clck.port, config->clck.pin, 1);
                bUtilDelayUS(1);
                bHalGPIO_WritePin(config->clck.port, config->clck.pin, 0);

                bUtilDelayUS(1);
                bHalGPIO_WritePin(config->clck.port, config->clck.pin, 1);
                bUtilDelayUS(1);
                bHalGPIO_WritePin(config->clck.port, config->clck.pin, 0);

                break;
            default:
                break;
        }
        int v = (data_temp << 8) / 256;
        bTM7711Callback(v);

        // b_log("bTM7711Callback %d \r\n", v);
    }
}
/**
 * \}
 */

/**
 * \}
 */

int b_tm7711_init()
{
    b_tm7711_driver.status  = 0;
    b_tm7711_driver.read    = NULL;
    b_tm7711_driver.write   = NULL;
    b_tm7711_driver.open    = NULL;
    b_tm7711_driver.close   = _b_tm7711_close;
    b_tm7711_driver.ctl     = NULL;
    b_tm7711_driver._hal_if = &b_tm7711_halif;
    // b_tm7711_halif_t *config = b_tm7711_driver._hal_if;
    bHalGPIO_Config(b_tm7711_halif.clck.port, b_tm7711_halif.clck.pin, B_HAL_GPIO_OUTPUT,
                    B_HAL_GPIO_NOPULL);
    bHalGPIO_Config(b_tm7711_halif.data.port, b_tm7711_halif.data.pin, B_HAL_GPIO_INPUT,
                    B_HAL_GPIO_PULLUP);
    // b_log("bTM7711_Init \r\n");
    return 0;
}

BOS_REG_POLLING_FUNC(_b_tm7711_polling);
bDRIVER_REG_INIT(b_tm7711_init);

/**
 * \}
 */

/**
 * \}
 */

/**
 * \}
 */

/************************ Copyright (c) 2020 Bean *****END OF FILE****/
